<template>
  <div class="admin">
    <el-card class="!border-none" shadow="never">
      <el-form class="mb-[-16px]" :model="formData" inline>
        <el-form-item label="租户名称">
          <el-input
            v-model="formData.name"
            class="w-[280px]"
            clearable
            @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item label="套餐" style="width: 250px">
          <el-select
            class="w-[280px]"
            v-model="formData.typeId"
            placeholder="请选择"
          >
            <el-option label="全部" value="" />
            <el-option
              v-for="(item, index) in types"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" style="width: 250px">
          <el-select
            class="w-[280px]"
            v-model="formData.status"
            placeholder="请选择"
          >
            <el-option label="全部" value="" />
            <el-option label="停用" :value="1" />
            <el-option label="正常" :value="0" />
          </el-select>
        </el-form-item>

        <el-form-item label="访问时间">
          <daterange-picker
            v-model:startTime="formData.beginTime"
            v-model:endTime="formData.endTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
      <el-button
        v-perms="['system:tenant:add']"
        type="primary"
        @click="handleAdd"
      >
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
      <div class="mt-4">
        <el-table :data="pager.lists" size="large">
          <el-table-column label="id" prop="id" min-width="60" />
          <el-table-column label="租户名称" prop="name" min-width="100" />
          <el-table-column label="账号" prop="userName" min-width="100" />
          <el-table-column label="名称" prop="nickName" min-width="100" />
          <el-table-column
            label="手机号"
            prop="mobile"
            show-tooltip-when-overflow
            min-width="120"
          />
          <el-table-column
            label="域名"
            prop="domain"
            show-tooltip-when-overflow
            min-width="100"
          />
          <el-table-column
            label="租户类型"
            prop="typeName"
            show-tooltip-when-overflow
            min-width="100"
          />

          <el-table-column label="过期时间" prop="expireTime" min-width="180" />

          <el-table-column label="状态" min-width="100">
            <template #default="{ row }">
              <el-tag class="ml-2" :type="row.status == '1' ? 'danger' : ''">
                {{ row.status == "1" ? "停用" : "正常" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-perms="['system:tenant:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                v-if="row.id != 1"
                v-perms="['system:tenant:del']"
                type="danger"
                link
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="tenant">
import { typeList, pageTenant, delTenant } from "@/api/system/tenant";

import { useDictOptions } from "@/layout/hooks/useDictOptions";
import { usePaging } from "@/layout/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
defineOptions({
  name: "SystemTeantList"
});
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 表单数据
const formData = reactive({
  name: "",
  typeId: undefined,
  beginTime: undefined,
  endTime: undefined,
  status: ""
});
//套餐数据
const types = ref(<any>[]);
const showEdit = ref(false);
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: pageTenant,
  params: formData
});

const getTenantTypes = async () => {
  let re = await typeList({});
  console.log("获取租户类型数据", re.data);
  types.value = re.data;
};

const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add", types.value);
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit", types.value);
  editRef.value?.setFormData(data);
};

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await delTenant([id]);
  feedback.msgSuccess("删除成功");
  getLists();
};

onMounted(() => {
  getTenantTypes();
  getLists();
});
</script>
